package com.example.demo.Test;

/**
 * 多线程练习
 *
 * @author Create by 崔少良 2018-06-08 下午 1:20
 */
public class ThreadTest {
    public static void main(String[] args) {
        MultithreadingTest_3();
    }
    //多线程练习1
    public static void MultithreadingTest(){
        Runner1 r1 = new Runner1();
        Runner2 r2 = new Runner2();
//        Thread(Runnable target) 分配新的 Thread 对象。
        Thread thread1 = new Thread(r1);
        Thread thread2 = new Thread(r2);
        thread1.start();
        thread2.start();
        thread1.run();
        thread2.run();
    }
    //多线程练习2
    public static void MultithreadingTest_2(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("进入Runner1运行状态——————————" + i);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("进入Runner2运行状态==========" + i);
                }
            }
        }).start();
    }
    //多线程练习3
    public static void MultithreadingTest_3(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("进入1运行状态——————————" + i);
                }
            }
        }) {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("进入2运行状态==========" + i);
                }
                super.run();
            }
        }.start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("进入3运行状态********************************" + i);
                }
            }
        }){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("进入4运行状态+++++++++++++++++++++++++++++++++++++++++++++++" + i);
                }
            }
        }.start();
    }
}
//线程1类
class Runner1 implements Runnable { // 实现了Runnable接口，jdk就知道这个类是一个线程

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("进入Runner1运行状态——————————" + i);
        }
    }
}
//线程2类
class Runner2 implements Runnable { // 实现了Runnable接口，jdk就知道这个类是一个线程
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("进入Runner2运行状态==========" + i);
        }
    }
}